<!-- START OF FUNCTIONS -->

<script type="text/javascript">

<?php
	// Copy a php array into a javascript array.
	$array_JS="usrList";

	printf("%s = new Array();\n",$array_JS);

	for($i=0; $i < sizeof($userList); $i++)
	{
		printf("%s[%s]= new Array();\n",$array_JS, $i);

				foreach($userList[$i]['User'] as $key => $value)
				{
					if ($key == 'id' ||$key == 'firstname' || $key == 'lastname' || $key == 'company_id'){
						printf("%s[%s].%s =%s;\n",$array_JS, $i, $key,
							is_string($value) ?  "\"".addslashes($value)."\"" : $value);
					}
				}
		
	}
?>

function updateUserList(companyId){
	for (i = document.getElementById("UserList").length; i > 0; i--)
		document.getElementById("UserList").options[i - 1] = null;
	if (companyId == 0)
		{
			new_elt = new Option("Aucun", "");
			document.getElementById("UserList").options[document.getElementById("UserList").length] = new_elt;
		}
	else
		{
			for (i = 0; i < usrList.length; i++)
			{
				if (usrList[i]['company_id'] == companyId)
				{
					str = "" + usrList[i]['lastname'] +" "+ usrList[i]['firstname'];
					new_elt = new Option(str, usrList[i]['id']);
					document.getElementById("UserList").options[document.getElementById("UserList").length] = new_elt;
				}
			}
		}
}

function toggleLayer( whichLayer, show ) {
	var elem, vis;
	if(document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
		elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	vis = elem.style;
	if (show) {
		vis.display = 'block';
	} else {
		vis.display = 'none';
	}
}

if (document.getElementById)
{
	if(navigator.appName.substring(0,3) == "Net")
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = Pos_Souris;
	//window.onload = depl_img;
}

function Pos_Souris(p)
{
	x = (navigator.appName.substring(0,3) == "Net") ? p.pageX : event.x+document.body.scrollLeft;
	y = (navigator.appName.substring(0,3) == "Net") ? p.pageY : event.y+document.body.scrollTop;
}

function affiche_img (url)
{
	document.getElementById("img_box").style.display="block";
	document.getElementById("img_box").style.left= x+50;
	document.getElementById("img_box").style.top= y-10; 
	contenu = "<img src='"+url+"'>";

	document.getElementById("img_box").innerHTML=contenu;
}

function cacher_img ()
{
	document.getElementById("img_box").style.display="none";
}

</script>

<!-- END OF FUNCTIONS -->

<?php echo $html->link(__('add_stakeholder', true), "javascript:toggleLayer('test', true)"); ?><br />
<div id="test" style="display:none;" class="largebloc2">
<table>
	<?php echo $form->create('Users', array('action' => 'lists')); ?>
	<?php echo $form->hidden('Role.project_id', array ('value' => $project_id)); ?>
	<?php echo $form->hidden('Role.project_name', array ('value' => $project_name)); ?>
	<tr>
		<td><?php echo $form->label(__('company', true).' :'); ?></td>
		<td><?php echo $form->select('Role.company_id', $format->buildClientArr($clientList), null,
						array('id'=>'ClientList', 'onChange'=>'javascript:updateUserList(this.options[this.options.selectedIndex].value)', 'style'=>'width:150px;'),
						$showEmpty = 'None'); ?></td>
		<?php //echo $form->error('company_id', 'Please select a company.'); ?>
	</tr>
	<tr>
		<td><?php echo $form->label(__('user', true).' :'); ?></td>
		<td><?php echo $form->select('Role.user_id', array(), null,
						array('id'=>'UserList','style'=>'width:150px;'),
						$showEmpty = 'None'); ?></td>
		<td><?php if ($rights_add) echo $link->projectLink($project_id, $project_name, __('add_user', true), "/users/adduser"); ?></td>
	</tr>
	<tr>
		<td><?php echo $form->label(__('profile', true).' :'); ?></td>
		<td><?php echo $form->select('Role.status_id', $format->buildStatusArr($statusList), null,
						array('id'=>'StatusList', 'style'=>'width:150px;'),
						false); ?></td>
	</tr>
	<tr>
		<td><?php echo $form->label(__('fast_access', true).' :'); ?></td>
		<td><?php echo $form->radio('Role.access', array('1'=>__('yes', true), '2'=>__('no', true)), Null, array()); ?></td>
	</tr>
	<tr>
		<td><?php echo $form->submit(__('validate', true)); ?></td>
		<td><?php echo $html->link(__('cancel', true), ''); ?></td>
	</tr>
	<?php echo $form->end(); ?>
</table>
</div>
<br />
<?php
	$companyList = array('Smile');
	foreach ($object['0']['Role'] as $a_role)
	{
		if (!in_array($a_role['User']['Company']['name'], $companyList))
			{
				array_push($companyList, $a_role['User']['Company']['name']);
			}
      	}
?>
<h2><?php __('stakeholders_list'); ?></h2>
<div class="largebloc1">
<?php foreach ($companyList as $currentCompany) { ?>
	<br /><h3><?php echo $currentCompany ?></h3>
	<?php echo $html->link(__('send_mail_stakeholders', true).' '.$currentCompany, "mailto:?cc=".$format->getUsersMailAdress($object['0']['Role'], $currentCompany)); ?>
	<br />
	<table width="100%" border="0" cellspacing="0" cellpadding="2">
	<?php 
		echo $html->tableHeaders(array(__('last_name', true), __('first_name', true), __('profil', true), __('mail', true), __('mobile_phone', true), __('phone', true), __('fast_access', true), 'Actions'), 'class="details"');
		foreach ($object['0']['Role'] as $currentUser) {
			if ($currentUser['User']['Company']['name'] == $currentCompany) {
				foreach ($statusList as $currentStatus) {
					if ($currentStatus['Status']['id'] == $currentUser['status_id'])
						$curstat = $currentStatus['Status']['name'];
				}
				echo $html->tableCells(array($html->link($currentUser['User']['lastname'], '/'.$project_name.'/'.$project_id.'/users/view/'. $currentUser['User']['id'], array('onMouseOver'=>'javascript:affiche_img('.$currentUser['User']['image'].')')),
									$link->projectLink($project_id, $project_name, $currentUser['User']['firstname'], '/users/view/'. $currentUser['User']['id']),
									$curstat,
									$html->link($currentUser['User']['mail'], "mailto:".$currentUser['User']['mail']),
									$currentUser['User']['mobile'],
									$currentUser['User']['phone'],
									($currentUser['access'] == 'yes') ? __('yes', true) : __('no', true),
									($rights_actions == true) ? ($link->projectLink($project_id, $project_name, __('edit', true), '/users/viewrole/'. $currentUser['User']['id']).' '.$html->link(__('delete', true), '/'.$project_name.'/'.$project_id.'/users/delrole/'.$currentUser['User']['id'], NULL, 'Are you sure ?')) : "-"),
								 'class="odd"', 'class="even"');
			}
		}
	?>
	</table>
<?php } ?>
</div>
<div id="img_box"></div>